package org.wutopia.labcloud.domain.uaa.service.impl.dubbo;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.wutopia.labcloud.domain.uaa.config.websocket.WebSocketHandler;
import org.wutopia.labcloud.domain.uaa.dao.UserNotificationDao;
import org.wutopia.labcloud.domain.uaa.dao.entity.UserNotification;
import org.wutopia.labcloud.service.api.service.dubbo.DubboUserService;
import org.wutopia.labcloud.service.api.service.dubbo.dto.input.UserInsertNotificationInputDTO;

import java.io.IOException;


@DubboService
@Slf4j
@AllArgsConstructor
public class DubboUserServiceImpl implements DubboUserService {

    private UserNotificationDao userNotificationDao;

    @Override
    public boolean sendOnLineMsg(Long userId, String msg)  {
        try {
            return WebSocketHandler.sendMsg(userId, msg);
        } catch (IOException e) {
            log.error(e.getLocalizedMessage());
            return false;
        }
    }

    @Override
    public void sendOffLineNotification(UserInsertNotificationInputDTO dto) {
        UserNotification userNotification = new UserNotification();
        userNotification.setUserId(dto.getUserId());
        userNotification.setContent(dto.getContent());
        userNotification.setTitle(dto.getTitle());
        userNotification.setIsRead(false);
        userNotificationDao.insert(userNotification);
    }
}
